package com.ftn;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * User: Jack Fu
 * Date: 2024/9/5
 * Time: 14:19
 * To change this template use File | Settings | File Templates.
 * Description:
 */
public class PlaneObj extends GameObj{

    // 记录碰撞补给的次数
    public int times = 1;

    Boss1Obj boss1Obj = new Boss1Obj();
    Boss2Obj boss2Obj = new Boss2Obj();

    int removeX = -1000;

    public PlaneObj() {
    }

    public PlaneObj(GameWin gameWin) {
        this.setImage(GameUtils.planeImage);
        setWidth(128);
        setHeight(128);
        setX(235);
        setY(650);
        setSpeed(5);
        setGameWin(gameWin);

        this.gameWin.addMouseMotionListener(new MouseAdapter() {
            /**
             * {@inheritDoc}
             *
             * @param e
             * @since 1.6
             */
            @Override
            public void mouseMoved(MouseEvent e) {
                /*setX(e.getX() - 128);
                setY(e.getY() - 128);*/
                PlaneObj.this.x = e.getX() - 64;
                PlaneObj.this.y = e.getY() - 64;

                /*if (e.getX() < 64) {
                    // 防止飞机出界
                    PlaneObj.this.x = 0;
                }
                if (e.getX() > 600 - 128) {
                    PlaneObj.this.x = 600 - 128;
                }
                if (e.getY() < 64) {
                    PlaneObj.this.y = 0;
                }
                if (e.getY() > 800 - 128) {
                    PlaneObj.this.y = 800 - 128;
                }*/

            }
        });
    }

    public PlaneObj(int x, int y, int width, int height, Image image, int speed, GameWin gameWin) {
        super(x, y, width, height, image, speed, gameWin);
    }

    public PlaneObj(int x, int y, Image image, int speed) {
        super(x, y, image, speed);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        // 碰撞检测
        // 我方飞机和敌方小飞机碰撞后，敌方飞机消失，我方飞机也消失
        GameUtils.diji1Objs.forEach(obj -> {
            if (this.getRec().intersects(obj.getRec())) {
                // 添加爆炸对象
                ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());
                GameUtils.explodeObjs.add(explodeObj);
                GameUtils.removeObjs.add(explodeObj);

                // 现将元素移除到界面之外
                obj.setX(-100);
                this.setX(removeX);
                GameUtils.removeObjs.add(obj);
                GameUtils.removeObjs.add(this);
                GameWin.gameState = 3;
            }
        });

        // 我方飞机和敌方大飞机碰撞后，两者都消失
        GameUtils.diji2Objs.forEach(obj -> {
            if (this.getRec().intersects(obj.getRec())) {
                // 添加爆炸对象
                ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());
                GameUtils.explodeObjs.add(explodeObj);
                GameUtils.removeObjs.add(explodeObj);

                // 现将元素移除到界面之外
                obj.setX(-100);
                this.setX(removeX);
                GameUtils.removeObjs.add(obj);
                GameUtils.removeObjs.add(this);
                GameWin.gameState = 3;
            }
        });
        // 我方飞机和敌方飞机子弹碰撞后，两者消失
        GameUtils.dijizidanObjs.forEach(obj -> {
            if (this.getRec().intersects(obj.getRec())) {
                // 添加爆炸对象
                ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());
                GameUtils.explodeObjs.add(explodeObj);
                GameUtils.removeObjs.add(explodeObj);

                // 现将元素移除到界面之外
                obj.setX(-100);
                this.setX(removeX);
                GameUtils.removeObjs.add(obj);
                GameUtils.removeObjs.add(this);
                GameWin.gameState = 3;
            }
        });

        // 与boss1发生碰撞, 飞机消失
        if (GameWin.boss1ObjFinal != null) {
            if (this.getRec().intersects(GameWin.boss1ObjFinal.getRec())) {
                // 添加爆炸对象
                ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());
                GameUtils.explodeObjs.add(explodeObj);
                GameUtils.removeObjs.add(explodeObj);

                // 现将元素移除到界面之外
                this.setX(removeX);

                GameUtils.removeObjs.add(this);
                GameWin.gameState = 3;
            }
        }

        // 与boss2发生碰撞，飞机消失
        if (GameWin.boss2ObjFinal != null) {
            if (this.getRec().intersects(GameWin.boss2ObjFinal.getRec())) {
                // 添加爆炸对象
                ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());
                GameUtils.explodeObjs.add(explodeObj);
                GameUtils.removeObjs.add(explodeObj);

                // 现将元素移除到界面之外
                this.setX(removeX);

                GameUtils.gameObjs.remove(this);
                GameWin.gameState = 3;
            }
        }

        // 与boss1子弹发生碰撞， 都消失
        GameUtils.boss1BulletObjs.forEach(obj -> {
            if (this.getRec().intersects(obj.getRec())) {
                // 添加爆炸对象
                ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());
                GameUtils.explodeObjs.add(explodeObj);
                GameUtils.removeObjs.add(explodeObj);

                // 现将元素移除到界面之外
                obj.setX(removeX);
                this.setX(removeX);

                GameUtils.gameObjs.remove(obj);
                GameUtils.gameObjs.remove(this);
                GameWin.gameState = 3;
            }
        });

        // 与boss2子弹发生碰撞, 都消失
        GameUtils.boss2BulletObjs.forEach(obj -> {
            if (this.getRec().intersects(obj.getRec())) {
                // 添加爆炸对象
                ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());
                GameUtils.explodeObjs.add(explodeObj);
                GameUtils.removeObjs.add(explodeObj);

                // 现将元素移除到界面之外
                obj.setX(removeX);
                this.setX(removeX);

                GameUtils.gameObjs.remove(obj);
                GameUtils.gameObjs.remove(this);
                GameWin.gameState = 3;
            }
        });

        // 补给碰撞
        GameUtils.giftObjs.forEach(obj -> {
            if (this.getRec().intersects(obj.getRec())) {
                // 现将元素移除到界面之外
                obj.setX(-removeX);
                GameUtils.removeObjs.add(obj);

                times++;
            }
        });

        // 与大boss碰撞, 飞机消失
        if (GameWin.bossObjFinal != null) {
            if (this.getRec().intersects(GameWin.bossObjFinal.getRec())) {
                // 添加爆炸对象
                ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());
                GameUtils.explodeObjs.add(explodeObj);
                GameUtils.removeObjs.add(explodeObj);

                // 现将元素移除到界面之外
                this.setX(removeX);

                GameUtils.gameObjs.remove(this);
                GameWin.gameState = 3;
            }
        }

        // 与大boss子弹碰撞， 飞机消失
        for (BossBulletObj bossBulletObj : GameUtils.bossBulletObjs) {
            if (this.getRec().intersects(bossBulletObj.getRec())) {
                // 添加爆炸对象
                ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());
                GameUtils.explodeObjs.add(explodeObj);
                GameUtils.removeObjs.add(explodeObj);

                // 现将元素移除到界面之外
                this.setX(removeX);

                GameUtils.gameObjs.remove(this);

                GameWin.gameState = 3;
            }
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
